package com.dongdongshop.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbAddress;
import com.dongdongshop.pojo.TbAreas;
import com.dongdongshop.pojo.TbCities;
import com.dongdongshop.pojo.TbProvinces;
import com.dongdongshop.service.TbAddressService;
import com.dongdongshop.service.TbAreasService;
import com.dongdongshop.service.TbCitiesService;
import com.dongdongshop.service.TbProvincesService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("address")
public class AddressController {
    @Reference
    private TbAddressService tbAddressService ;
    @Reference
    private TbProvincesService tbProvincesService ;
    @Reference
    private TbCitiesService tbCitiesService;
    @Reference
    private TbAreasService tbAreasService;
    @RequestMapping("toaddre")
    public String toShow2(){
        return "home-setting-address";
    }
    //查询收获地址
    @RequestMapping("getAddressInfo")
    @ResponseBody
    public Result toGetInfo(){
        //有bug 没有根据登录态进查询
         List<TbAddress> tbAddress =  tbAddressService.selectAll();
         return Result.build(ConstantEnum.SUCCESS).setResult(tbAddress);
    }
    //获取省
    @RequestMapping("getProinceInfo")
    @ResponseBody
    public Result getProinceInfo(){
        List<TbProvinces> provincesList =  tbProvincesService.selectProvinces();
        return Result.build(ConstantEnum.SUCCESS).setResult(provincesList);
    }
    //获取市
    @RequestMapping("getCityInfo/{city}")
    @ResponseBody
    public Result getCityInfo(@PathVariable("city")String city){
         List<TbCities> tbCitiesList = tbCitiesService.selectByCity(city);
         return Result.build(ConstantEnum.SUCCESS).setResult(tbCitiesList);
    }
    //获取县
    @RequestMapping("getAreaInfo/{area}")
    @ResponseBody
    public Result getAreaInfo(@PathVariable("area")String area){
         List<TbAreas> areasList = tbAreasService.selectByArea(area);
         return Result.build(ConstantEnum.SUCCESS).setResult(areasList);
    }
    //增加地址
    @RequestMapping("addAddressInfo")
    @ResponseBody
    public Result addAddressInfo(TbAddress tbAddress){
        Boolean b = tbAddressService.addAll(tbAddress);
        return Result.build(ConstantEnum.SUCCESS).setResult(b);
    }
    //根据ID查询省份
    @RequestMapping("getProvincName/{provinceId}")
    public Result getProvincName(@PathVariable("provinceId")String provinceId){
         TbProvinces provinces =tbProvincesService.selectGetName(provinceId);
         return Result.build(ConstantEnum.SUCCESS).setResult(provinces.getProvince());
    }
    //根据Id查询所在的城市
    @RequestMapping("getCityName/{city}")
    @ResponseBody
    public Result getCityName(@PathVariable("city")String city){
         TbCities cities = tbCitiesService.selectGetCityName(city);
            return  Result.build(ConstantEnum.SUCCESS).setResult(cities.getCity());
    }
    //根据ID获取所在的县
    @RequestMapping("getTownName/{town}")
    @ResponseBody
    public Result getTownName(@PathVariable("town")String town){
         TbAreas tbAreas = tbAreasService.selectGetTownName(town);
         return Result.build(ConstantEnum.SUCCESS).setResult(tbAreas.getArea());
    }
}
